package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.base.ZjbBaseActivity;
import com.hdcx.Minfresh.bean.OrderDetailWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.hdcx.Minfresh.view.ListView4ScrollView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class OrderDetialReActivity extends ZjbBaseActivity implements View.OnClickListener {

    private ListView4ScrollView mListOrderDetail;
    String id = "";
    ImageView img_main, img_style;
    TextView tv_name, tv_intro, tv_intro_2, tv_units, tv_money, tv_person, tv_phone, tv_address, tv_style, tv_info;
    TextView tv_bz, tv_totle_money, tv_realy_money, tv_xdsj, tv_ddbh, tv_zffs, tv_jylsh;
    private List<OrderDetailWrapper.Glist> mGlist = new ArrayList<>();
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detial_re);
        init();
    }

    @Override
    protected void initIntent() {
        id = getIntent().getStringExtra("id");
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void findID() {
        mListOrderDetail = (ListView4ScrollView) findViewById(R.id.listOrderDetail);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_style = (TextView) findViewById(R.id.tv_style);
        tv_intro = (TextView) findViewById(R.id.tv_intro);
        tv_info = (TextView) findViewById(R.id.tv_info);
        tv_intro_2 = (TextView) findViewById(R.id.tv_intro_2);
        tv_units = (TextView) findViewById(R.id.tv_units);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_person = (TextView) findViewById(R.id.tv_person);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        img_main = (ImageView) findViewById(R.id.img_main);
        img_style = (ImageView) findViewById(R.id.img_style);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_bz = (TextView) findViewById(R.id.tv_bz);
        tv_totle_money = (TextView) findViewById(R.id.tv_totle_money);
        tv_realy_money = (TextView) findViewById(R.id.tv_realy_money);
        tv_xdsj = (TextView) findViewById(R.id.tv_xdsj);
        tv_ddbh = (TextView) findViewById(R.id.tv_ddbh);
        tv_zffs = (TextView) findViewById(R.id.tv_zffs);
        tv_jylsh = (TextView) findViewById(R.id.tv_jylsh);
    }

    @Override
    protected void initViews() {
        mAdapter = new MyAdapter();
        mListOrderDetail.setAdapter(mAdapter);
        setView();
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.tv_left).setOnClickListener(this);
    }

    private void setView() {
        UserInfoBean user = ShpfUtil.getObject(ShpfUtil.LOGIN);
        showLoading();
        JSONObject json = new JSONObject();
        try {
            json.put("uid", user.getUserinfo().getId());
            json.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.OITEM)
                .content(json.toString())
                .build()
                .execute(new Callback<OrderDetailWrapper>() {
                    @Override
                    public OrderDetailWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, OrderDetailWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(OrderDetailWrapper response) {
                        closeLoading();
                        if (response.getStatus() == 1) {
                            AppUtil.showPic(response.getOrder().getStaus_img(), img_style);
                            tv_style.setText(response.getOrder().getStatus_msg());
                            tv_info.setText(response.getOrder().getStatus_info());
                            tv_person.setText("收货人:" + response.getAddress().getName());
                            tv_phone.setText(response.getAddress().getMobile());
                            tv_address.setText(response.getAddress().getAddress());
                            tv_ddbh.setText(response.getOrder().getOrder_sn());
                            tv_bz.setText(response.getOrder().getNotes().equals("") ? "未填写" : response.getOrder().getNotes());
                            tv_xdsj.setText(response.getOrder().getAdd_time());
                            tv_zffs.setText(response.getOrder().getPayname());
                            tv_jylsh.setText(response.getOrder().getOut_trade_sn());
                            tv_totle_money.setText("￥" + response.getOrder().getGoods_amount());
                            tv_realy_money.setText("￥" + response.getOrder().getOrder_amount());
                            mGlist.addAll(response.getGlist());
                            mAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finishTo();
                break;
        }
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mGlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View inflate = getLayoutInflater().inflate(R.layout.item_activity_collect, null);
            ImageView img_main = (ImageView) inflate.findViewById(R.id.img_main);
            TextView tv_name = (TextView) inflate.findViewById(R.id.tv_name);
            TextView tv_intro = (TextView) inflate.findViewById(R.id.tv_intro);
            TextView tv_intro_2 = (TextView) inflate.findViewById(R.id.tv_intro_2);
            TextView tv_money = (TextView) inflate.findViewById(R.id.tv_money);
            TextView tv_units = (TextView) inflate.findViewById(R.id.tv_units);
            OrderDetailWrapper.Glist glist = mGlist.get(position);
            AppUtil.showPic(glist.getGoods_img(), img_main);
            tv_name.setText(glist.getGoods_title());
            tv_intro.setText(glist.getGoods_title());
            tv_intro_2.setText("规格：" + glist.getGoods_etalon() + glist.getGoods_units());
            tv_money.setText("￥" + glist.getGoods_price() + "/" + glist.getGoods_units());
            tv_units.setText(glist.getOrder_quantity() + glist.getGoods_units());
            return inflate;
        }
    }
}
